<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 内容管理
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button
                    type="primary"
                    icon="el-icon-plus"
                    class="handle-del mr10"
                    @click="addContent"
                >新增内容页</el-button>
               <el-select v-model="query.contentStatus" clearable placeholder="审核状态" class="handle-select mr10">
                    <el-option label="未发布" :value="1"></el-option>
                    <el-option label="审核中" :value="2"></el-option>
                    <el-option label="审核不通过" :value="3"></el-option>
                    <el-option label="已发布" :value="4"></el-option>
                </el-select>
                <el-input v-model="query.name" placeholder="内容名称" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <!-- <el-table-column type="selection" width="55" align="center"></el-table-column> -->
                <el-table-column type="index" label="序号" width="55" align="center"></el-table-column>
                <el-table-column prop="contentName" label="内容名称"></el-table-column>
                <el-table-column prop="contentDesc" label="内容描述"></el-table-column>
                <el-table-column prop="logoImg" label="logo展示">
                    <template slot-scope="scope">
                        <img :src="scope.row.logoImg" class="tableImg" alt="">
                    </template>
                </el-table-column>
                <el-table-column label="链接" prop="contentLink"></el-table-column>
                <el-table-column label="审核描述" prop="auditingDescribe"></el-table-column>
                <el-table-column label="状态" align="center">
                    <template slot-scope="scope">
                        {{scope.row.contentStatus==4?'发布成功':scope.row.contentStatus==3?'审核失败':scope.row.contentStatus==2?'审核中':'未发布'}}
                    </template>
                </el-table-column>
                <el-table-column prop="createTime" label="创建时间" width="170"></el-table-column>
                <el-table-column label="操作" width="300" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.$index, scope.row)"
                        >修改标题</el-button>
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="toEditContent(scope.row)"
                        >编辑内容</el-button>
                        <el-button
                            type="text"
                            class="success"
                            icon="el-icon-upload el-icon--right"
                            @click="toRelease(scope.row)"
                        > {{scope.row.contentStatus==4?'再次发布':"发布"}} </el-button>
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="内容名称">
                    <el-input v-model="form.contentName"></el-input>
                </el-form-item>
                <el-form-item label="链接">
                    <el-input v-model="form.contentLink"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>


         <el-dialog title="新增" :visible.sync="addDialog" width="30%">
            <el-form ref="submitFrom" :model="submitForm" :rules="rules" label-width="100px">
                <el-form-item label="内容名称" prop="contentName">
                    <el-input v-model="submitForm.contentName"></el-input>
                </el-form-item>
                <el-form-item label="内容描述" prop="contentDesc">
                    <el-input v-model="submitForm.contentDesc"></el-input>
                </el-form-item>
                <el-form-item label="logo展示" prop="logoImg">
                    <div class="left_imgs">
                        <el-upload
                            class="avatar-uploader"
                            action="#"
                            :auto-upload="false"
                            :show-file-list="false"
                            :on-change="getBg"
                            :on-remove="handleRemove"
                            :http-request="uploadImg"
                        >
                            <img v-if="submitForm.logoImg" :src="submitForm.logoImg" class="avatar" />
                            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                            <div class="img_loading">
                                <p v-if='!submitForm.logoImg'>添加背景图片</p> 
                            </div>
                        </el-upload>
                    </div>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addDialog = false">取 消</el-button>
                <el-button type="primary" @click="addSure('add')">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { getContentList,deleteContent,editContent,addContent  } from 'api/content';
import { uploadImg } from 'api/upload';
import { setStore} from '@/utils/store';
export default {
    name: 'basetable',
    data() {
        return {
            query: {
                contentStatus:null,
                pageIndex: 1,
                pageSize: 10
            },
            tableData: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            pageTotal: 0,
            form: {},
            idx: -1,
            id: -1,
            submitForm:{
                logoImg:''
            },
            addDialog:false,
            activeRow:{},
            rules:{
                contentName: [{ required: true, message: '请输入内容名称', trigger: 'blur' }],
                contentDesc: [{ required: true, message: '请填写内容描述', trigger: 'blur' }],
                logoImg:[
                    { required: true, message: '请选择图片', trigger: 'change' }
                ]
            }
        };
    },
    created() {
        this.getData();
    },
    activated(){
        this.getData();
    },
    methods: {
         // 背景图
        getBg(val){
            let infoId = this.$route.query.id;
            var file = val.raw;
            var formdata=new FormData();// 创建form对象
            formdata.append('file',file,file.name);
            uploadImg(formdata).then(res=>{
                if(res){
                    this.submitForm.logoImg = res;
                    // this.$forceUpdate();
                }
            })
        },
        uploadImg() {},
        handleRemove() {},
        //发布
        toRelease(row){
            this.$confirm('确定发布吗？', '提示', {
            }).then(()=>{
                editContent({
                    id:row.id,
                    contentStatus:2,
                }).then(res=>{
                    if(res){
                        this.$message.success('已发布，待审核');
                        this.getData();
                    }
                })
            }).catch(err=>{})
            
        },
        toEditContent(row){
            getContentList({
                "pageIndex":1,
                'pageSize':100,
                condition:{
                    contentStatus:4,
                    contentType:2,
                }
            }).then(res=>{
                let result = res.result;
                if(result && result.length){
                    setStore('secondList',result)
                }
                this.$router.push({
                    path:'/editContent',
                    query:{
                        id:row.id,
                        contentType:row.contentType,
                    }
                })
            }).catch(err=>{
                this.$message.error('网络错误，请联系管理员')
            })

           
        },
        //确认
        addSure(type){
             this.$refs.submitFrom.validate(valid => {
                if(valid){
                    let param = {
                        ...this.submitForm,
                        contentType:1,  //1为一级页面，2为二级页面
                    }
                    if(type=='add'){
                        addContent(param).then(res=>{
                            if(res){
                                this.$message.success('新增成功');
                                this.addDialog = false;
                                this.getData()
                            }
                        })
                    }
                }else{
                    return false;
                }
             })
          
        },
        // 获取 easy-mock 的模拟数据
        getData() {
            getContentList({
                "pageIndex":this.query.pageIndex,
                'pageSize':this.query.pageSize,
                condition:{
                    ...this.query,
                    contentType:1, //一级页面
                }
            }).then(res=>{
                let result = res.result;
                this.tableData = result;
                this.pageTotal = res.total;
            })
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'pageIndex', 1);
            this.getData();
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    deleteContent({
                        id:row.id
                    }).then(res=>{
                        if(res){
                            this.$message.success('删除成功');
                            this.getData();
                        }
                    })
                })
                .catch(() => {});
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        addContent() {
            this.submitForm = {
                logoImg:''
            };
            this.addDialog = true
            // const length = this.multipleSelection.length;
            // let str = '';
            // this.delList = this.delList.concat(this.multipleSelection);
            // for (let i = 0; i < length; i++) {
            //     str += this.multipleSelection[i].name + ' ';
            // }
            // this.$message.error(`删除了${str}`);
            // this.multipleSelection = [];
        },
        // 编辑操作
        handleEdit(index, row) {
            this.idx = index;
            this.form =JSON.parse(JSON.stringify(row));
            this.editVisible = true;
        },
        // 保存编辑
        saveEdit() {
            let params = {
                ...this.form
            }
            editContent(params).then(res=>{
                if(res){
                    this.editVisible = false;
                    this.$message.success(`修改成功`);
                    this.getData();
                }
            })
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        }
    }
};
</script>

<style scoped  lang="less">
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.success{
    color:#67C23A;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
 .left_imgs {
    width: 84px;
    height: 82px;
    position: relative;
    margin-right: 10px;
    .img_loading {
        width: 100%;
        height: 100%;
        position: absolute;
        background: rgba(0, 0, 0, 0.6);
        top: 0;
        font-size: 12px;
        color: #ffffff;
        display: none;
        justify-content: center;
        align-items: center;
    }
    .el-upload:hover .img_loading {
        display: flex;
    }
    .upload_text {
        font-size: 12px;
        line-height: 17px;
    }
    /deep/ .avatar-uploader .el-upload {
        border: 1px dashed #ff8244;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    /deep/ .avatar-uploader .el-upload:hover {
        // border-color: #FF8244;
    }
    /deep/.avatar-uploader-icon {
        font-size: 22px;
        color: #ff8244;
        width: 82px;
        height: 82px;
        // line-height: 98px;
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;
    }
    /deep/.avatar {
        width: 62px;
        height: 62px;
        display: block;
    }
}
.tableImg{
    width:auto;
    height:60px;
}
</style>
